package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.bean.Orders;
import com.example.bean.OrdersMerchant;
import com.example.bean.OrdersUser;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface OrdersMapper extends BaseMapper<Orders> {

    /**
     * 第一版
     */
    @Select("select * from orders")
    List<Orders> getOrdersAll();

    //用户查询订单列表
    @Select("select * from orders where u_id = #{uid}")
    List<Orders> getOrdersAllUser(int uid);

    //商家查询订单列表
    @Select("select * from orders where m_id = #{mid}")
    List<Orders> getOrdersAllMerchant(int mid);

    //用户根据订单号精准查询订单列表
    @Select("select * from orders where order_number = #{orderNumber} and u_id = #{uid}")
    List<Orders> findOrderByOrderNumberUser(String orderNumber, int uid);

    //商家根据订单号精准查询订单列表
    @Select("select * from orders where order_number = #{orderNumber} and m_id = #{mid}")
    List<Orders> findOrderByOrderNumberMerchant(String orderNumber, int mid);

    //用户根据商品名模糊查询订单列表
    @Select("SELECT * FROM orders WHERE m_name LIKE CONCAT('%',#{mName},'%') AND u_id = #{uid}")
    List<Orders> findOrderByMNameUser(String mName, int uid);

    //商家根据商品名模糊查询订单列表
    @Select("SELECT * FROM orders WHERE m_name LIKE CONCAT('%',#{mName},'%') AND m_id = #{mid}")
    List<Orders> findOrderByMNameMerchant(String mName, int mid);

    //删除订单
    @Delete("Delete from orders where order_number = #{orderNumber}")
    Boolean deleteOrderByOrderNumber(String orderNumber);


    /**
     * 第二版（商家）
     */
    //查询订单
    @Select("SELECT o.*,g.g_img,g.g_name,g.price " +
            "FROM goods g,orders o " +
            "WHERE o.m_id = #{mid} " +
            "AND g.g_id = o.g_id")
    List<OrdersMerchant> findOrdersAllMerchant2(int mid);

    //商家根据订单号精准查询订单列表
    @Select("SELECT o.*,g.g_img,g.g_name,g.price " +
            "FROM goods g,orders o " +
            "WHERE o.order_number = #{orderNumber} " +
            "AND o.m_id = #{mid} " +
            "AND g.g_id = o.g_id")
    List<OrdersMerchant> findOrderByOrderNumberMerchant2(String orderNumber, int mid);

    //商家根据商品名模糊查询订单列表
    @Select("SELECT o.*,g.g_img,g.g_name,g.price " +
            "FROM goods g,orders o " +
            "WHERE g.g_name LIKE CONCAT('%',#{gName},'%') " +
            "AND o.m_id = #{mid} " +
            "AND g.g_id = o.g_id")
    List<OrdersMerchant> findOrderByNameMerchant2(String gName, int mid);

    //商家修改用户收货地址
    @Update("UPDATE orders " +
            "SET o_name = #{oName},o_phone = #{oPhone},o_address = #{oAddress},c_number = #{cNumber} " +
            "WHERE u_id = #{uid} " +
            "AND g_id = #{gid} " +
            "AND m_id = #{mid}")
    Boolean modifyOrderMerchant2(int uid, int gid, int mid, String oName, String oPhone, String oAddress, String cNumber);

    //商家改变用户订单状态
    @Update("UPDATE orders SET o_state = #{oState}, c_number= #{cNumber}" +
            "WHERE u_id = #{uid} " +
            "AND g_id = #{gid} " +
            "AND m_id = #{mid}")
    Boolean changeOrderState2(String oState, int uid, int gid, int mid, String cNumber);

    /**
     * 第二版（用户）
     */
    //查询订单
    @Select("SELECT g.g_name,g.g_img,g.g_describe,g.price,o.* " +
            "FROM orders o,goods g " +
            "WHERE o.u_id = #{uid} " +
            "AND o.g_id = g.g_id")
    List<OrdersUser> findOrdersAllUser2(int uid);

    //用户根据订单号精准查询订单列表
    @Select("SELECT g.g_name,g.g_img,g.g_describe,g.price,o.* " +
            "FROM orders o,goods g " +
            "WHERE o.order_number = #{orderNumber} " +
            "AND o.u_id = #{uid} " +
            "AND g.g_id = o.g_id")
    List<OrdersUser> findOrderByOrderNumberUser2(String orderNumber, int uid);

    //用户根据商品名模糊查询订单列表
    @Select("SELECT g.g_name,g.g_img,g.g_describe,g.price,o.* " +
            "FROM orders o,goods g " +
            "WHERE g.g_name LIKE CONCAT('%',#{gName},'%') " +
            "AND o.u_id = #{uid} " +
            "AND g.g_id = o.g_id")
    List<OrdersUser> findOrderByNameUser2(String gName, int uid);

    //用户根据订单状态查询订单
    @Select("SELECT g.g_name,g.g_img,g.g_describe,g.price,o.* " +
            "FROM orders o,goods g " +
            "WHERE o.o_state = #{oState} " +
            "AND o.u_id = #{uid} " +
            "AND o.g_id = g.g_id")
    List<OrdersUser> findOrderByStateUser2(String oState, int uid);

    // 用户改变订单状态
    @Update("UPDATE orders SET o_state = #{orderstate} WHERE u_id = #{uid} AND order_number=#{orderNumber}")
    int ChangeStateUser(String orderstate, int uid, String orderNumber);

    //查找最新插入的订单号
    @Select("SELECT order_number FROM orders where u_id =#{uid} ORDER BY TIME DESC LIMIT 0,1")
    String findNewstOrder(int uid);

    //减少商品库存
    @Update("UPDATE  goods SET g_inventory=g_inventory-1 WHERE g_id=#{gid}")
    boolean reInventory(int gid);
}
